#include "EngineHeader.h"
#include "GUIHeader.h"

namespace LSH
{
namespace GUI
{

//------------------------------------------------------------------------------
EventSet::EventSet(void):_muted(false)
{
}

//------------------------------------------------------------------------------
EventSet::~EventSet(void)
{
}

//------------------------------------------------------------------------------
void EventSet::addEvent(const String& name)
{
    Event* pEvent = CM_NEW Event(name);
    if(isEventPresent(name))
    {
        // error
    }
    _events.insert(eventValue(name, pEvent));
}

//------------------------------------------------------------------------------
void EventSet::removeEvent(const String& name)
{
    eventItr itr = _events.find(name);
    if(itr != _events.end())
    {
        CM_DELETE (itr->second);
        _events.erase(itr);
    }
}

//------------------------------------------------------------------------------
void EventSet::removeAllEvents()
{
    eventItr itr = _events.begin();
    eventItr endItr = _events.end();
    while(itr != endItr)
    {
        CM_DELETE (itr->second);
        itr++;
    }
    _events.clear();
}

//------------------------------------------------------------------------------
bool EventSet::isEventPresent(const String& name)
{
    return (_events.find(name) != _events.end());
}

//------------------------------------------------------------------------------
bool EventSet::subscribeEvent(const String& name, SlotFunctorBase* slot)
{
    Event* pEvent = getEventObject(name);
    if(pEvent)
    {
       return pEvent->subscribe(slot);
    }
    return false;
}

//------------------------------------------------------------------------------
void EventSet::fireEvent(const String& name, EventArgs& args)
{
    fireEvent_impl(name, args);
}

//------------------------------------------------------------------------------
bool EventSet::isMuted() const 
{
    return _muted;
}

//------------------------------------------------------------------------------
void EventSet::setMutedState(bool setting)
{
    _muted = setting;
}

//------------------------------------------------------------------------------
Event* EventSet::getEventObject(const String& name, bool autoAdd)
{
    eventItr itr = _events.find(name);
    eventItr endItr = _events.end();
    if(itr != endItr)
    {
        return itr->second;
    }
    else
    {
        if(autoAdd)
        {
            Event* pEvent = CM_NEW Event(name);
            _events.insert(eventValue(name, pEvent));
            return pEvent;
        }
        else
        {
            return NULL;
        }
        
    }
}

//------------------------------------------------------------------------------
void EventSet::fireEvent_impl(const String& name, EventArgs& args)
{
    Event* pEvent = getEventObject(name);

    if(pEvent && !_muted)
    {
        (*pEvent)(args);
    }

}

} // GUI
} // LSH
